package com.zlt.appparking.shiro.realm;

import com.zlt.appparking.entity.AppUser;
import com.zlt.appparking.entity.UserWallet;
import com.zlt.appparking.mapper.UserWalletMapper;
import com.zlt.appparking.service.AppUserService;
import com.zlt.appparking.shiro.config.JWTToken;
import com.zlt.appparking.utils.JWTUtil;
import com.zlt.appparking.utils.StringUtil;
import com.zlt.appparking.vo.ActiveUser;
import com.zlt.appparking.vo.UserWalletVO;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AppUserRealm extends AuthorizingRealm {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Override
    public String getName() {
        return "userRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 默认的授权方法，检查请求中是否包含权限或者角色的时候会被调用
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        /*ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        List<RoleVO> roleVOList = principal.getRoleVOList();
        List<PermissionVO> permissionVOList = principal.getPermissionVOList();
        roleVOList.forEach( r -> {
            simpleAuthorizationInfo.addRole(r.getRname());
        });
        permissionVOList.forEach( p -> {
            simpleAuthorizationInfo.addStringPermission(p.getPdesc());
        });

        return simpleAuthorizationInfo;*/
        return null;
    }

    /**
     * 认证方法 调用subject.login的时候会被调用
     * @param t
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken t) throws AuthenticationException {
        String token = (String) t.getPrincipal();
        if (StringUtil.isBlank(token)){
            throw new AuthenticationException("token无效");
        }
        String username = JWTUtil.getUsername(token);
        if (StringUtil.isBlank(username)){
            throw new AuthenticationException("token无效");
        }
        AppUser appUser = appUserService.selectAppUserByUsername(username);
        if (appUser == null){
            throw new AuthenticationException("token无效");
        }
        if (!JWTUtil.verify(token,username,appUser.getAuPassword())){
            throw new AuthenticationException("token无效");
        }
        ActiveUser activeUser = new ActiveUser();
        BeanUtils.copyProperties(appUser,activeUser);
//        查询用户的钱包
        List<UserWalletVO> userWalletVO = appUserService.selectUserWalletByAuId(appUser.getAuId());
        activeUser.setUserWalletVO(userWalletVO);



//        // 可以查询角色和权限信息
//        List<RoleVO> roleVOS = appUserService.selectRolesByUid(activeUser.getUid());
//        activeUser.setRoleVOS(roleVOS);
//        // 可以根据角色将权限信息查询出来
//        List<PermissionVO> permissionVOList = appUserService.selectPermissionByRoles(roleVOS);
//        activeUser.setPermissionVOS(permissionVOList);
//        // 查询部门信息
//        List<DepartmentVO> departmentVOS = appUserService.selectDepartments(appUser.getUid());
//        activeUser.setDepartmentVOS(departmentVOS);
        return new SimpleAuthenticationInfo(activeUser,token,getName());
    }
}
